package com.gxuwz.medical.domain.family;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Timestamp;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;

import com.gxuwz.medical.database.DbUtil;
import com.gxuwz.medical.exception.DbException;

public class FamilyMember {
	
	private String membercode;
	
	private String cmscode;
	
	private String name;
	
	private String relative;
	
	private String familycode;
	
	private String idcard;

	private String sex;

	private Timestamp birthday;

	private String nation;

	private String health;

	private String educationallevel;

	private String memberattribute;

	private String farmtype;

	private String married;

	private String workunit;

	private String phone;

	public String getMembercode() {
		return membercode;
	}

	public void setMembercode(String membercode) {
		this.membercode = membercode;
	}

	public String getCmscode() {
		return cmscode;
	}

	public void setCmscode(String cmscode) {
		this.cmscode = cmscode;
	}

	public String getName() {
		return name;
	}

	public void setName(String name) {
		this.name = name;
	}

	public String getRelative() {
		return relative;
	}

	public void setRelative(String relative) {
		this.relative = relative;
	}

	public String getFamilycode() {
		return familycode;
	}

	public void setFamilycode(String familycode) {
		this.familycode = familycode;
	}

	public String getIdcard() {
		return idcard;
	}

	public void setIdcard(String idcard) {
		this.idcard = idcard;
	}

	public String getSex() {
		return sex;
	}

	public void setSex(String sex) {
		this.sex = sex;
	}

	public Timestamp getBirthday() {
		return birthday;
	}

	public void setBirthday(Timestamp birthday) {
		this.birthday = birthday;
	}

	public String getNation() {
		return nation;
	}

	public void setNation(String nation) {
		this.nation = nation;
	}

	public String getHealth() {
		return health;
	}

	public void setHealth(String health) {
		this.health = health;
	}

	public String getEducationallevel() {
		return educationallevel;
	}

	public void setEducationallevel(String educationallevel) {
		this.educationallevel = educationallevel;
	}

	public String getMemberattribute() {
		return memberattribute;
	}

	public void setMemberattribute(String memberattribute) {
		this.memberattribute = memberattribute;
	}

	public String getFarmtype() {
		return farmtype;
	}

	public void setFarmtype(String farmtype) {
		this.farmtype = farmtype;
	}

	public String getMarried() {
		return married;
	}

	public void setMarried(String married) {
		this.married = married;
	}

	public String getWorkunit() {
		return workunit;
	}

	public void setWorkunit(String workunit) {
		this.workunit = workunit;
	}

	public String getPhone() {
		return phone;
	}

	public void setPhone(String phone) {
		this.phone = phone;
	}


	public FamilyMember(String membercode, String cmscode, String name,
			String relative, String familycode, String idcard, String sex,
			String birthday, String nation, String health,
			String educationallevel, String memberattribute, String farmtype,
			String married, String workunit, String phone) {
		super();
		this.membercode = membercode;
		this.cmscode = cmscode;
		this.name = name;
		this.relative = relative;
		this.familycode = familycode;
		this.idcard = idcard;
		this.sex = sex;
		try {
			this.birthday = stringToTimestamp(birthday.toString());
		} catch (java.text.ParseException e) {
			e.printStackTrace();
		}
		this.nation = nation;
		this.health = health;
		this.educationallevel = educationallevel;
		this.memberattribute = memberattribute;
		this.farmtype = farmtype;
		this.married = married;
		this.workunit = workunit;
		this.phone = phone;
	}
	
	public FamilyMember(){
		
	}
	
	@Override
	public String toString() {
		return "FamilyMember [membercode=" + membercode + ", cmscode="
				+ cmscode + ", name=" + name + ", relative=" + relative
				+ ", familycode=" + familycode + ", idcard=" + idcard
				+ ", sex=" + sex + ", birthday=" + birthday + ", nation="
				+ nation + ", health=" + health + ", educationallevel="
				+ educationallevel + ", memberattribute=" + memberattribute
				+ ", farmtype=" + farmtype + ", married=" + married
				+ ", workunit=" + workunit + ", phone=" + phone + "]";
	}

	public static Timestamp stringToTimestamp(String dateStr)
			throws java.text.ParseException {

		SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
		Calendar cal = Calendar.getInstance();
		try {
			Date date = (Date) sdf.parse(dateStr);
			date.getTime();
			cal.setTime(date);
			return new Timestamp(cal.getTimeInMillis());
		} catch (ParseException e) {
			e.printStackTrace();
		}
		cal.setTime(new Date());
		return new Timestamp(cal.getTimeInMillis());
	}
	
	private void loadDB(String familycode) throws SQLException {
		Connection conn = null;
		PreparedStatement pstmt = null;
		ResultSet rs = null;
		try {
			conn = DbUtil.getConn();
			pstmt = conn
					.prepareStatement("select * from t_family_member where membercode=?");
			pstmt.setString(1, this.membercode);
			rs = pstmt.executeQuery();
			if (rs.next()) {
				this.membercode = rs.getString("membercode");
				this.cmscode = rs.getString("cmscode");
				this.name = rs.getString("name");
				this.relative = rs.getString("relative");
				this.familycode = rs.getString("familycode");
				this.idcard = rs.getString("idcard");
				this.sex = rs.getString("sex");
				this.birthday = rs.getTimestamp("birthday");
				this.nation = rs.getString("nation");
				this.health = rs.getString("health");
				this.educationallevel = rs.getString("educationallevel");
				this.memberattribute = rs.getString("memberattribute");
				this.farmtype = rs.getString("farmtype");
				this.married = rs.getString("married");
				this.workunit = rs.getString("workunit");
				this.phone = rs.getString("phone");
			}
		} catch (SQLException e) {
			throw e;
		} finally {
			DbUtil.close(rs, pstmt, conn);
		}
	}
	
	public FamilyMember(String membercode) throws DbException {
		try {
			this.membercode = membercode;
			loadDB(membercode);
		} catch (SQLException e) {
			throw new DbException("加载id=" + membercode + "行政区域记录失败", e);
		}
	}
	
	/**
	 * 添加家庭档案的时候顺便把这个户主添加进去
	 * @param name
	 * @param familycode
	 * @throws Exception
	 */
	public void addFamilyholder(FamilyMember familyMember) throws Exception {
		Connection conn = null;
		try {
			// 保存导数据库
			conn = DbUtil.getConn();
			conn.setAutoCommit(false);
			// 调用插入数据库私有方法
			this.saveToDB(conn);
			conn.commit();
		} catch (Exception e) {
			conn.rollback();
			throw e;
		} finally {
			DbUtil.close(conn);
		}

	}
	
	public void addFamily(String membercode, String cmscode, String name,
			String relative, String familycode, String idcard, String sex,
			Object birthday, String nation, String health,
			String educationallevel, String memberattribute, String farmtype,
			String married, String workunit, String phone) throws Exception {
		Connection conn = null;
		try {
			// 初始化属性
			this.membercode = membercode;
			this.cmscode = cmscode;
			this.name = name;
			this.relative = relative;
			this.familycode = familycode;
			this.idcard = idcard;
			this.sex = sex;
			try {
				this.birthday = stringToTimestamp(birthday.toString());
			} catch (java.text.ParseException e) {
				e.printStackTrace();
			}
			this.nation = nation;
			this.health = health;
			this.educationallevel = educationallevel;
			this.memberattribute = memberattribute;
			this.farmtype = farmtype;
			this.married = married;
			this.workunit = workunit;
			this.phone = phone;
			// 保存导数据库
			conn = DbUtil.getConn();
			conn.setAutoCommit(false);
			// 调用插入数据库私有方法
			this.saveToDB(conn);
			conn.commit();
		} catch (Exception e) {
			conn.rollback();
			throw e;
		} finally {
			DbUtil.close(conn);
		}

	}
	
	private void saveToDB(Connection connection) throws SQLException {
		PreparedStatement pstmt = null;
		try {

			StringBuffer sqlBuff = new StringBuffer();
			sqlBuff.append("insert into t_family_member(membercode,cmscode,name,relative,familycode,idcard,sex,"
					+ "birthday,nation,health,educationallevel,memberattribute,"
					+ "farmtype,married,workunit,phone)");
			sqlBuff.append("values(?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?)");
			String sql = sqlBuff.toString();
			pstmt = connection.prepareStatement(sql);
			int i = 1;
			pstmt.setString(i++, this.membercode);
			pstmt.setString(i++, this.cmscode);
			pstmt.setString(i++, this.name);
			pstmt.setString(i++, this.relative);
			pstmt.setString(i++, this.familycode);
			pstmt.setString(i++, this.idcard);
			pstmt.setString(i++, this.sex);
			pstmt.setTimestamp(i++, this.birthday);
			pstmt.setString(i++, this.nation);
			pstmt.setString(i++, this.health);
			pstmt.setString(i++, this.educationallevel);
			pstmt.setString(i++, this.memberattribute);
			pstmt.setString(i++, this.farmtype);
			pstmt.setString(i++, this.married);
			pstmt.setString(i++, this.workunit);
			pstmt.setString(i++, this.phone);
			// 以executeUpdate()执行SQL
			pstmt.executeUpdate();
		} catch (SQLException e) {
			throw e;
		} finally {
			// 只关闭PrepareStatement资源
			if (pstmt != null) {
				pstmt.close();
			}

		}

	}
	
	public void edit(String membercode, String cmscode, String name,
			String relative, String familycode, String idcard, String sex,
			Object birthday, String nation, String health,
			String educationallevel, String memberattribute, String farmtype,
			String married, String workunit, String phone) throws SQLException {
		Connection conn = null;
		try {
			this.membercode = membercode;
			this.cmscode = cmscode;
			this.name = name;
			this.relative = relative;
			this.familycode = familycode;
			this.idcard = idcard;
			this.sex = sex;
			try {
				this.birthday = stringToTimestamp(birthday.toString());
			} catch (java.text.ParseException e) {
				e.printStackTrace();
			}
			this.nation = nation;
			this.health = health;
			this.educationallevel = educationallevel;
			this.memberattribute = memberattribute;
			this.farmtype = farmtype;
			this.married = married;
			this.workunit = workunit;
			this.phone = phone;
			conn =DbUtil.getConn();
			conn.setAutoCommit(false);
			this.updateToDB(conn);
			conn.commit();
		} catch (SQLException e) {
			conn.rollback();
			throw e;
		} finally {
			DbUtil.close(conn);
		}
	}

	private void updateToDB(Connection conn) throws SQLException {
		PreparedStatement pstmt = null;
		try {
			// 定义构造update SQL语句字符串变量sql
			String sql = "update t_family_member set cmscode=?,name=?,relative=?,familycode=?,idcard=?,sex=?,"
					+ "birthday=?,nation=?,health=?,educationallevel=?,memberattribute=?,"
					+ "farmtype=?,married=?,workunit=?,phone=? where membercode=?";
			// 创建执行带动态参数的SQL的PreparedStatement pstmt
			pstmt = conn.prepareStatement(sql);
			// 设置动态参数对应的值
			int i = 1;
			pstmt.setString(i++, this.cmscode);
			pstmt.setString(i++, this.name);
			pstmt.setString(i++, this.relative);
			pstmt.setString(i++, this.familycode);
			pstmt.setString(i++, this.idcard);
			pstmt.setString(i++, this.sex);
			pstmt.setTimestamp(i++, this.birthday);
			pstmt.setString(i++, this.nation);
			pstmt.setString(i++, this.health);
			pstmt.setString(i++, this.educationallevel);
			pstmt.setString(i++, this.memberattribute);
			pstmt.setString(i++, this.farmtype);
			pstmt.setString(i++, this.married);
			pstmt.setString(i++, this.workunit);
			pstmt.setString(i++, this.phone);
			pstmt.setString(i++, this.membercode);
			int count = pstmt.executeUpdate();

		} catch (SQLException e) {
			throw e;
		} finally {
			DbUtil.close(pstmt);
		}

	}
	
	public void del(String membercode)throws SQLException{

		Connection conn=null;
       try{
    	   //1:对象属性赋值
    	    this.membercode =membercode;
    	    conn=DbUtil.getConn();
    	   //2；调用delFromDB方法
    	    delFromDB(conn);
       }catch(SQLException e){
    	   throw e;
       }finally{
    	   DbUtil.close(conn);
       }
		
	}
	private void delFromDB(Connection conn)throws SQLException{
		PreparedStatement pstmt=null;
		try{
			//定义构造插入SQL语句字符串变量sql
			String  sql="delete from t_family_member where membercode=?";
			 //创建执行带动态参数的SQL的PreparedStatement pstmt
			 pstmt=conn.prepareStatement(sql);
			 //设置动态参数对应的值
			 int index=1;
			 pstmt.setString(index++, this.membercode);
		
			int count=pstmt.executeUpdate();
			
		}catch(SQLException e){
			throw e;
		}finally{
			DbUtil.close(pstmt);
			
		}
		
		
	}
}
